package com.javacoo.sftp.starter;

import org.apache.commons.pool2.impl.DefaultEvictionPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javacoo.sftp.client.api.SftpClient;
import com.javacoo.sftp.client.config.SftpPoolConfig;
import com.javacoo.sftp.client.internal.DefaultSftpClient;
import com.javacoo.sftp.client.internal.SftpTemplatePool;

/**
 * 自动配置类
 * <li></li>
 * @author duanyong@javacoo.com
 * @date 2020/5/14 13:40
 */
@Configuration                                                                                                                              
@EnableConfigurationProperties(value = SftpClientProperties.class)
@ConditionalOnClass(SftpClient.class)
@ConditionalOnProperty(prefix = SftpClientProperties.PREFIX, value = SftpClientProperties.ENABLED, matchIfMissing = true)
public class SftpClientAutoConfiguration {
    /** sftp连接池配置参数*/
	@Autowired                                                                                                                              
    private SftpClientProperties sftpClientProperties;
	
	@Bean
	@ConditionalOnMissingBean(SftpClient.class)
	public SftpClient createSftpClient() throws Exception {
        SftpPoolConfig sftpPoolConfig = createSftpPoolConfig(sftpClientProperties);
        SftpTemplatePool sftpTemplatePool = new SftpTemplatePool(sftpClientProperties.getClients(),sftpPoolConfig);
		return DefaultSftpClient.builder().sftpTemplatePool(sftpTemplatePool).build();
	}
    /**
     * 创建SftpPoolConfig
     * <li></li>
     * @author duanyong@javacoo.com
     * @date 2020/5/14 13:49
     * @param properties:sftp连接池配置参数
     * @return: com.javacoo.sftp.client.config.SftpPoolConfig
     */
    private SftpPoolConfig createSftpPoolConfig(SftpClientProperties properties) {
        SftpClientProperties.Pool pool = properties.getPool();
        return new SftpPoolConfig.Builder()
            .maxTotal(pool.getMaxTotal())
            .maxIdle(pool.getMaxIdle())
            .minIdle(pool.getMinIdle())
            .lifo(pool.isLifo())
            .fairness(pool.isFairness())
            .maxWaitMillis(pool.getMaxWaitMillis())
            .minEvictableIdleTimeMillis(pool.getMinEvictableIdleTimeMillis())
            .evictorShutdownTimeoutMillis(pool.getEvictorShutdownTimeoutMillis())
            .softMinEvictableIdleTimeMillis(pool.getSoftMinEvictableIdleTimeMillis())
            .numTestsPerEvictionRun(pool.getNumTestsPerEvictionRun())
            .evictionPolicy(null)
            .evictionPolicyClassName(DefaultEvictionPolicy.class.getName())
            .testOnCreate(pool.isTestOnCreate())
            .testOnBorrow(pool.isTestOnBorrow())
            .testOnReturn(pool.isTestOnReturn())
            .testWhileIdle(pool.isTestWhileIdle())
            .timeBetweenEvictionRunsMillis(pool.getTimeBetweenEvictionRunsMillis())
            .blockWhenExhausted(pool.isBlockWhenExhausted())
            .jmxEnabled(pool.isJmxEnabled())
            .jmxNamePrefix(pool.getJmxNamePrefix())
            .jmxNameBase(pool.getJmxNameBase())
            .retryCount(pool.getRetryCount())
            .build();
    }
}
